package view;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import server.Log;
import server.Log.directOpt;

/**
 * Class contains system defenitions.
 */
public class Settings implements Serializable
{
	/**
	 * Serial version ID - for serialization
	 */
	private static final long serialVersionUID = -400244250039297898L;

	/********************************* System ************************************/
	/**
	 * User mode (STANDART/ADVANCED)
	 */ 
	static protected UserMode userMode = UserMode.STANDART;
	
	/**
	 * Settings file
	 */
	static private String fileName = "settings";
	
	/********************************* Logger ************************************/
	/**
	 * Default number of the rows in the logger table
	 */
	public static final int DEFAULT_LOGGER_WINDOW_DEPTH = 15;
	
	/**
	 * Number of the rows in the logger table
	 */
	public static int windowDepth = DEFAULT_LOGGER_WINDOW_DEPTH;
	
	/**
	 * Default number of history days
	 */
	public static final int DEFAULT_LOGGER_HISTORY_DEPTH = 1;
	
	/**
	 * Actual number of history days
	 */
	public static Integer historyDepth = new Integer(DEFAULT_LOGGER_HISTORY_DEPTH);

	/**
	 * Denied sessions number
	 */
	public static Integer statsDenied   = 0;
	
	/**
	 * Accepted sessions number
	 */
	public static Integer statsAccepted = 0;
	
	/**
	 * Flag if the logger in static or dynamuc update mode
	 */
	public static boolean loggerStaticMode = true;
	
	/* Options for the filtering */
	
	/**
	 * Type of the actual filtering
	 */
	public static FilterType currentFilterType = FilterType.ALL;
	
	/**
	 * Port of the actual filtering
	 */
	public static Integer filterPort 		= null;
	
	/**
	 * IP of the actual filtering
	 */
	public static String filterIP    		= null;
	
	/**
	 * Direction of the actual filtering
	 */
	public static Log.directOpt filterDir	= Log.directOpt.IN;
	
	/**
	 * Protocol of the actual filtering
	 */
	public static String filterProtocol		= null;
	
	/********************************* Rules ************************************/
	/**
	 * Default number of the rows in the rules table
	 */
	public static final int DEFAULT_RULES_WINDOW_DEPTH = 13;

	/**
	 *  Current ruleset id.
	 */
	public static String ruleset = "default";
	
	/**
	 * Actual rules in IN direcion number
	 */
	public static int inRulesNumber = 0;
	
	/**
	 * Actual rules in OUT direcion number
	 */
	public static int outRulesNumber = 0;
	
	/********************************* Protocols *****************************/
	/**
	 * Default number of the rows in the protocols table
	 */
	public static final int DEFAULT_PROTOCOL_WINDOW_DEPTH = 13;
	
	/**
	 * Write all needed settings to the file
	 * @throws IOException - the problem with the file writing occures
	 */	
	protected static void save() throws IOException
	{
		/* Open output stream */
		FileOutputStream fos = new FileOutputStream(fileName);
		ObjectOutputStream oos = new ObjectOutputStream(fos);

		/* Save the data */
	    oos.writeObject(Settings.filterIP);
	    oos.writeObject(Settings.filterProtocol);
	    oos.writeObject(Settings.historyDepth);
	    oos.writeObject(Settings.filterDir);
	    oos.writeObject(Settings.filterProtocol);	
		
		/* Close output stream */	    
	    oos.flush();
	    oos.close();
	    fos.close();
	}
	
	/**
	 * Read all needed settings from the file 
	 * @throws IOException				- the problem with the file writing occures
	 * @throws ClassNotFoundException	- the proble with type of reading object occures
	 */
	protected static void load() throws IOException, ClassNotFoundException
	{
		/* Open input stream */
		FileInputStream fis = new FileInputStream(fileName);
		ObjectInputStream ois = new ObjectInputStream(fis);

		/* Save the data */
		Settings.filterIP = (String) ois.readObject();
		Settings.filterProtocol = (String)ois.readObject();
		Settings.historyDepth = (Integer)ois.readObject();
		Settings.filterDir = (directOpt)ois.readObject();
		Settings.filterProtocol = (String)ois.readObject();
		
		/* Close output stream */
		ois.close();
		fis.close();	
	}
}
